public class Solution {
    public int[] plusOne(int[] digits) {
        int value = digits[digits.length - 1] + 1;
        if (value < 10) {
            digits[digits.length - 1] = value;
            return digits;
        }

        for (int i = digits.length - 1; i >= 0; i--) {
            int tmp = digits[i] + 1;
            digits[i] = tmp % 10;
            if (tmp < 10) {
                return digits;
            }
        }

        if (digits[0] != 0) {
            return digits;
        }

        int[] newDigits = new int[digits.length + 1];
        newDigits[0] = 1;
        for (int i = 0; i < digits.length; i++) {
            newDigits[i + 1] = digits[i];
        }
        return newDigits;
    }

    public int[] plusOne2(int[] digits) {
        for (int i = digits.length - 1; i >= 0; i--) {
            int tmp = digits[i] + 1;
            digits[i] = tmp % 10;
            if (tmp < 10) {
                return digits;
            }
        }

        int[] newDigits = new int[digits.length + 1];
        newDigits[0] = 1;
        return newDigits;
    }
}